package cn.tp.ksoft.civil_driver.module.order;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

import cn.tp.ksoft.civil_driver.DriverBaseActivity;
import cn.tp.ksoft.civil_driver.R;
import cn.tp.ksoft.civil_driver.bean.ResultBean;
import cn.tp.ksoft.civil_driver.request.DriverOrderRequest;
import cn.tp.ksoft.civil_driver.request.RequestModules;
import cn.tp.ksoft.civil_driver.utils.Constants;

/**
 * Created by zhusong on 16/5/2.
 */
public class RefuseOrderActivity extends DriverBaseActivity {

    private DriverOrderRequest request;
    private SparseArray<CheckBox> mReasons;
    private EditText mOtherDetailEdt;
    private Long orderId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.driver_activity_reject_reasons);
        updateTitle(getString(R.string.driver_reject_order));
        Intent intent = getIntent();
        if (null != intent) {
            orderId = intent.getLongExtra(Constants.KEY_ORDER_ID, -1);
        }
        mOtherDetailEdt = (EditText) findViewById(R.id.edt_others);
        int[] checkIds = {R.id.cb_busy_today, R.id.cb_car_repairing,
        R.id.cb_out_of_local, R.id.cb_feel_bad, R.id.cb_rest,
        R.id.cb_others};
        mReasons = new SparseArray<>(checkIds.length);
        for (int i = 0,len = checkIds.length; i < len; i++) {
            mReasons.put(checkIds[i], (CheckBox) findViewById(checkIds[i]));
        }

        mReasons.get(R.id.cb_others).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mOtherDetailEdt.setEnabled(isChecked);
            }
        });

        request = new DriverOrderRequest(this, new WeakHandler(this));

        findViewById(R.id.btn_refuse).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringBuffer sb = new StringBuffer();

                for (int i = 0,len = mReasons.size(); i < len; i++) {
                    int id = mReasons.keyAt(i);
                    CheckBox cb = mReasons.get(id);
                    if (cb.isChecked()) {
                        if (cb.getId() == R.id.cb_others) {
                            sb.append(cb.getTag().toString()+":"+mOtherDetailEdt.getText());
                        } else {

                            sb.append(cb.getTag().toString()+",");
                        }
                    }
                }

                if (TextUtils.isEmpty(sb.toString())) {
                    Toast.makeText(RefuseOrderActivity.this, "请选择拒绝理由", Toast.LENGTH_SHORT).show();
                    return;
                }
                request.refuseOrder(orderId, sb.toString());

            }
        });
    }

    @Override
    protected void handleSuccessResult(int what, @NonNull ResultBean result) {
        switch (what) {
            case RequestModules.REQUEST_ORDER_REFUSE : {
                setResult(RESULT_OK);
                finish();
                break;
            }
        }
    }
}
